﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GTCAS.CommonLib.Entities;
using GTCAS.DataAccess.Common;
namespace GTCAS.DataAccess
{
    public class MenuDAO : ExtendDAO
    {
        public MenuDAO(long id_tinhthanh)
            : base(id_tinhthanh)
        {
        }
        /// <summary>
        /// Lay toan bo du lieu menu dang List
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public List<GT_MENU> GetListAll()
        {
            List<GT_MENU> result = null;

            BaseDAO adapter = new BaseDAO(this.ConnectionString);
            try
            {
                string command = "SELECT MENU_ID,MENU_NAME,URL,PARENT_MENU_ID,ORDINAL,NEW_URL FROM " + this.Schema + ".GT_MENU ";
                result = adapter.GetList<GT_MENU>(command, CommandType.Text, CustomConnectionState.CloseOnExit);

            }
            catch (Exception exc)
            {//Custom Error here

                throw exc;

            }
            finally { adapter.Dispose(); }
            return result;
        }
        /// <summary>
        /// Lay danh sach menu ma nguoi dung duoc phep thao tac
        /// </summary>
        /// <param name="id_user"></param>
        /// <returns></returns>
        public List<GT_MENU> GetListByUser(string username)
        {
            List<GT_MENU> result = null;

            BaseDAO adapter = new BaseDAO(this.ConnectionString);
            adapter.AddParameter(":uname", username);
            try
            {
                string command = "SELECT DISTINCT m.MENU_ID,m.MENU_NAME,m.URL,m.PARENT_MENU_ID,m.ORDINAL,NEW_URL FROM "
                    + this.Schema + ".GT_USERNAME u , "
                    + this.Schema + ".GT_USER_GROUP ug, "
                    + this.Schema + ".GT_SERCURITY_MENU gm, "
                    + this.Schema + ".GT_MENU m "
                    + " WHERE u.username = ug.user_name "
                    + " AND ug.group_id = gm.group_id "
                    + " AND m.menu_id = gm.menu_id "
                    + " AND u.username=:uname ";
                result = adapter.GetList<GT_MENU>(command, CommandType.Text, CustomConnectionState.CloseOnExit);

            }
            catch (Exception exc)
            {//Custom Error here

                throw exc;

            }
            finally { adapter.Dispose(); }
            return result;
        }
    }
}
